﻿
using Abp.Application.Services;
using Abp.Domain.Repositories;
using Abp.UI;
using ARchGL.Platform.Electrocar.Application.Dto;
using ARchGL.Platform.Electrocar.Core;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Abp.Linq.Extensions;
using System.Threading.Tasks;
using Abp.Threading;
using ARchGL.Platform.Electrocar.Application.Utils;
using System.Collections;
using ARchGL.Platform.Electrocar.Application.Enum;
using Abp.Timing;

namespace ARchGL.Platform.Electrocar.Application
{
    /// <summary>
    /// 系统服务 实现
    /// </summary>
    public class InstallAppService : ApplicationService, IInstallAppService
    {
        private readonly IRepository<Accounts, Guid> accountsRepository;
        private readonly IRepository<Departments, Guid> departmentsRepository;
        private readonly IRepository<CarCategorys, Guid> carCategorysRepository;
        private readonly IRepository<ModelAssemblys, Guid> modelAssemblysRepository;
        private readonly IRepository<ModelDirectorys, Guid> modelDirectorysRepository;
        private readonly IRepository<ModelPropertys, Guid> modelPropertysRepository;


        public InstallAppService(IRepository<Accounts, Guid> _accountsRepository, IRepository<Departments, Guid> _departmentsRepository,
            IRepository<ModelAssemblys, Guid> _modelAssemblysRepository,
            IRepository<ModelDirectorys, Guid> _modelDirectorysRepository,
            IRepository<ModelPropertys, Guid> _modelPropertysRepository,
            IRepository<CarCategorys, Guid> _carCategorysRepository)
        {
            accountsRepository = _accountsRepository;
            departmentsRepository = _departmentsRepository;
            modelAssemblysRepository = _modelAssemblysRepository;
            modelDirectorysRepository = _modelDirectorysRepository;
            modelPropertysRepository = _modelPropertysRepository;
            carCategorysRepository = _carCategorysRepository;
        }



        /// <summary>
        /// 初始化数据（密码咨询管理员）
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task Init(string password)
        {
            if (string.IsNullOrWhiteSpace(password) || !password.Contains("!@#123qwe"))
                throw new UserFriendlyException("密码错误，请重新输入");

            try
            {
                //AsyncHelper.RunSync(()=> asdfasdf());
                await asdfasdf();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #region 初始化数据
        private async Task asdfasdf()
        {
            var accountId = Guid.Parse("4d68853f-2456-4e64-9397-0bf33392c837");
            var account = await accountsRepository.FirstOrDefaultAsync(accountId);
            if (account == null)
            {
                account = new Accounts
                {
                    Id = accountId,
                    Name = "系统管理员",
                    Telephone = "13800138000",

                    Roles = "1",
                    IsDeleted = false,
                    TenantId = 1,
                    CreationTime = DateTime.Parse("2018-11-20")
                };
                await accountsRepository.InsertAsync(account);
            }

            //var department = new Departments
            //{
            //    Id = Guid.Parse("B64B64CC-969D-4A2F-A1AC-E4D1FBF6826F"),
            //    ParentId = Guid.Empty,
            //    Name = "研发部",
            //    Level = 1,
            //    Sort = 1,
            //    Description = "描述",
            //    IsDeleted = false,
            //    TenantId = 1,
            //    CreationTime = DateTime.Parse("2018-11-20")
            //};
            //await departmentsRepository.InsertAsync(department);

            //车型

            //车身

            //创建项目总成，目录与属性
            var templateResult = await new Init().CreatePropertys(Guid.Empty);//"5C76CEC7-412A-4586-B298-E150A8100000"

            foreach (var item in templateResult.Item1)
            {
                await modelAssemblysRepository.InsertAsync(item);
            }

            foreach (var item in templateResult.Item2)
            {
                await modelDirectorysRepository.InsertAsync(item);
            }

            foreach (var item in templateResult.Item3)
            {
                await modelPropertysRepository.InsertAsync(item);
            }

            //创建车型级别分类
            var carCategorysResult = await new Init().CreateCarCategorys("11BBBFA5-F86F-11E8-BC4A-E03F49F00000");
            foreach (var item in carCategorysResult)
            {
                await carCategorysRepository.InsertAsync(item);
            }
        }

        #endregion

    }
}
